#include <iostream>
using namespace std;
#include <vector>
#include <unordered_map>

class Solution
{
public:
    static bool containsNearbyDuplicate(vector<int>& nums, int k)
    {
        unordered_map<int, int> hash;
        for(int i=0; i<nums.size(); i++)
        {
            if(hash.count(nums[i]))
            {
                if(abs(i-hash[nums[i]]) <= k)
                    return true;
            }
            hash[nums[i]] = i;
        }
        return false;
    }
};